package com.three.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.three.entity.Inventory;
import com.three.entity.InventoryGoods;
import com.three.entity.Staff;
import com.three.mapper.BranchGoodsMapper;
import com.three.mapper.InventoryGoodsMapper;
import com.three.mapper.InventoryMapper;
import com.three.service.InventoryService;
import com.three.vo.AddInventoryVo;
import com.three.vo.BaoSunVo;
import com.three.vo.KeyboardVO;
import com.three.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author 袁大毛
 * * @date 2024/10/25 下午7:07
 * @描述
 */
@Service
public class InventoryServiceImpl implements InventoryService {
    @Autowired
    private InventoryMapper inventoryMapper;
    @Autowired
    private InventoryGoodsMapper inventoryGoodsMapper;
    @Autowired
    private BranchGoodsMapper branchGoodsMapper;
    @Autowired
    private HttpSession session;

    //获取盘点信息
    @Override
    public R listInventory(Integer currentPage, Integer pageSize, KeyboardVO keyboardVO) {
        Staff staff = (Staff) session.getAttribute("staff");
        Integer branchid = staff.getBranchid();
        //开启分页
        PageHelper.startPage(currentPage, pageSize);
        if (branchid!=12138){
            keyboardVO.setBranchId(branchid);
        }
        List<Inventory> list = inventoryMapper.getAllInventory(keyboardVO);
        //封装分页数据
        PageInfo<Inventory> pageInfo = new PageInfo<>(list);
        //System.out.println(list);
        //System.out.println(pageInfo+"!!!!!!!!!!!!!!!!!!!");
        return R.ok(pageInfo);
    }


    //获取所有详细盘点信息
    @Override
    public R listInventoryDetail(Integer invenid) {
        List<InventoryGoods> list = inventoryGoodsMapper.selectAllAndStaff(invenid);
        //System.out.println(pageInfo);
        return R.ok(list);
        //return null;
    }

    //              2：通过   1：等待审核   3：驳回
    //修改盘点状态,通过方法
    @Override
    @Transactional//事务
    public R updateStateYes(Integer invenid, Integer branchid) {
        //得到审核人id
        Staff staff = (Staff) session.getAttribute("staff");
        Integer staffId = staff.getStaffid();

        //1.首先修改盘点表状态，并且添加审核时间(现在时刻)
        Date date = new Date();
        int i = inventoryMapper.updateStateYes(invenid, date,staffId);

        if (i > 0) {
            /*  2.修改对应门店 该商品的的库存
                    2.1 先获取该商品id,和所在的部门，得到该商品盘点后的数目
                    2.2 根据商品id，修改对应库存*/
            List<InventoryGoods> inventoryGoods = inventoryGoodsMapper.selectByInvenId(invenid);
            for (int j = 0; j < inventoryGoods.size(); j++) {
                Integer newCounts = inventoryGoods.get(j).getInvennum();
                int i1 = branchGoodsMapper.updateCountsByBranchidAndGoodsid
                        (branchid, inventoryGoods.get(j).getGoodsid(),newCounts);
            }
            return R.ok("操作成功");
        } else {
            return R.error("操作失败");
        }
    }

    //驳回，修改状态为3 方法
    @Override
    @Transactional//事务
    public R updateStateNo(Integer invenid, String remarks) {
        //得到审核人id
        Staff staff = (Staff) session.getAttribute("staff");
        Integer staffId = staff.getStaffid();

        //审核时间
        Date date = new Date();
        int i = inventoryMapper.updateStateNo(invenid, date, remarks,staffId);
        if (i > 0) {
            return R.ok();
        } else {
            return R.error("操作失败");
        }
    }

    //获取所有补损商品,状态全部为2
    @Override
    @Transactional//事务
    public R getAllBaoSunGoods(Integer currentPage, Integer pageSize, BaoSunVo abc) {
        //开启分页
        PageHelper.startPage(currentPage, pageSize);
        //
        List<Inventory> list = inventoryMapper.getAllBaoSunGoods(abc);
        //封装分页数据
        PageInfo<Inventory> pageInfo = new PageInfo<>(list);
        return R.ok(pageInfo);
    }

    //检查是否有未审核的盘点单
    //     2：通过   1：等待审核   3：驳回
    @Override
    public R checkState() {
        List<Inventory> inventories = inventoryMapper.selectByState();
        //System.out.println(inventories);
        for (Inventory inventory : inventories){
            if (inventory.getState()==1){
                return R.error();
            }
        }
        return R.ok();
    }

    //添加盘点单
    //     2：通过   1：等待审核   3：驳回
    @Override
    @Transactional//事务
    public R addInventory(List<AddInventoryVo> listInventoryVo) {
        //System.out.println(listInventoryVo);
        Inventory inventory = new Inventory();
        //添加盘点单 inventory表
        Date inventime =new Date();//盘点时间
        Staff staff = (Staff) session.getAttribute("staff");
        Integer invenstaffid = staff.getStaffid();//盘点人员id
        Integer branchid = staff.getBranchid();//属于门店id
        Integer state=1;//等待审核状态
        inventory.setInventime(inventime);
        inventory.setInvenstaffid(invenstaffid);
        inventory.setBranchid(branchid);
        inventory.setState(state);
        //添加盘点单 inventory表
        int i = inventoryMapper.insterInventory(inventory);
        //System.out.println(i+"@#$@$"+inventory);
        if (i>0){
            //添加盘点单 inventory_goods表
            for (AddInventoryVo aa : listInventoryVo){
                int goodsid = (int) aa.getGoodsid();
                int invennum = (int) aa.getInvennum();
                int lossnum = (int) aa.getLossnum();
                InventoryGoods gg = new InventoryGoods();
                gg.setInvenid(inventory.getInvenid());
                gg.setGoodsid(goodsid);
                gg.setInvennum(invennum);
                gg.setLossnum(lossnum);
                int i1 = inventoryGoodsMapper.insertPanDian(gg);
                if (i1>0){

                    //System.out.println("添加盘点单成功");
                }else{
                    return R.error("添加盘点单失败");
                }
            }

        }
        return R.ok();
    }
}